<?php
	session_start();	//start session
	if(!isset($_SESSION['user_name'])){	//check if the user data is in the session
		header("location: login.php");	//if not redirect back to user
	}
?>


<html>
	<head>
		<title></title>
		<link rel="stylesheet" href="style.css">
		<script src="jquery-1.11.0.js"></script>
		<script src="gen.js"></script>
		
		<script>
			var currID;
			var viewID;
			var start=0;
			var end=15;
			var total_entries;
			
			var limit=15;
			//latest_table();
			//console.log(latest_table());

			//makes a synchronous call to the page u and return the 
			//result as object
			function syncAjax(u){

				 var obj=$.ajax(
				{
					url:u,
					async:false
			 	}
			);
				//console.log($.parseJSON(obj.responseText));
				return $.parseJSON(obj.responseText);
			}
			
			function edit(obj,id,event){
				var r=getUser(id);
				currID = id;
				if(r.result==0){
				document.getElementById("divStatus").innerHTML=r.message;
				$("#divStatus").text(r.message);	
					return;
				}


				//get the data from object r and put it in the form
				$("#firstname").prop("value", r.user.firstname);
				$("#lastname").prop("value", r.user.lastname);
				$("#user_name").prop("value", r.user.user_name);
				$("#password").prop("value", r.user.password);
				$("#position").prop("value", r.user.position);
				$("#status").prop("value", r.user.status);
				$("#date_created").prop("value", r.user.date_created);
				$("#phone_number").prop("value", r.user.phone_number);

				//show the form
				//find where the user clicked and store it in x and y
				var y=event.clientY;
				var x=event.clientX/2;
				//use x and y to set the location of the form
				$("#divView").hide();
				$("#divEdit").hide();
				$("#divAdd").hide();
				$("#divEdit").css("top",y);
				$("#divEdit").css("left",x);
				//display the form
				$("#divEdit").fadeIn(1000);
				// $("#addid").hide();
				$("#savid").show();
				
			}
			
			function view(obj,id,event){
				var r=getUser(id);
				viewID = id;
				if(r.result==0){
				document.getElementById("divStatus").innerHTML=r.message;
				$("#divStatus").text(r.message);	
					return;
				}


				//get the data from object r and put it in the form
				$("#f_name").prop("value", r.user.firstname);
				$("#l_name").prop("value", r.user.lastname);
				$("#u_name").prop("value", r.user.user_name);
				$("#p_word").prop("value", r.user.password);
				$("#p_s").prop("value", r.user.position);
				$("#s_t").prop("value", r.user.status);
				$("#d_created").prop("value", r.user.date_created);
				$("#p_number").prop("value", r.user.phone_number);

				//show the form
				//find where the user clicked and store it in x and y
				var y=event.clientY;
				var x=event.clientX/2;
				//use x and y to set the location of the form
				$("#divView").hide();
				$("#divEdit").hide();
				$("#divAdd").hide();
				$("#divView").css("top",152);
				$("#divView").css("left",402);
				$("#divView").fadeIn(1000);
				$("#viewid").show();
			}


			//returns a result object for one vaccine 
			function getUser(id){
				var u="user_action.php?cmd=1&id="+id;
				return syncAjax(u);
			}

			function search(){
				var tx=document.getElementById("txtSearch").value;
				console.log(tx);
				var u = "user_action.php?cmd=5&fn="+tx;
				r=syncAjax(u);
				search_table(r);
				
			}



			function add(){
				$("#divView").hide();
				$("#divEdit").hide();
				$("#divAdd").hide();
				var y=event.clientY;
				var x=event.clientX/3;
				$("#divAdd").css("top",152);
				$("#divAdd").css("left",402);
				$("#divAdd").fadeIn(1000);
				$("#addid").show();
				// $("#savid").hide();
			}


			
			function addNew(){
				//complete the url
				var fn=document.getElementById("fn").value;
				//var vn = $('#vaccine_name').val();
				var ln=document.getElementById("ln").value;
				var un=document.getElementById("un").value;
				var pw=document.getElementById("pw").value;
				var ps=document.getElementById("ps").value;
				var st=document.getElementById("st").value;
				var dc=document.getElementById("dc").value;
				var pn=document.getElementById("pn").value;
				var u = "user_action.php?cmd=4&fn="+fn+"&ln="+ln+"&un="+un+"&pw="+pw+"&ps="+ps+"&st="+st+"&dc="+dc+"&pn="+pn;	
				r=syncAjax(u);
				//$.getJSON(u,saveDone);
				r.result==1?alert("Saved"):alert("Save failed");
				latest_table();
				cancel_add();
			}

			//makes asynchronous call to the save page
			function save(){
				//complete the url
				var fn=document.getElementById("firstname").value;
				//var vn = $('#vaccine_name').val();
				var ln=document.getElementById("lastname").value;
				var un=document.getElementById("user_name").value;
				var pw=document.getElementById("password").value;
				var ps=document.getElementById("position").value;
				var st=document.getElementById("status").value;
				var dc=document.getElementById("date_created").value;
				var pn=document.getElementById("phone_number").value;
				var u = "user_action.php?cmd=3&fn="+fn+"&ln="+ln+"&un="+un+"&pw="+pw+"&ps="+ps+"&st="+st+"&dc="+dc+"&pn="+pn+"&id="+currID;
				r=syncAjax(u);
				$.getJSON(u,saveDone);
				//r.result==1?alert("Saved"):alert("Save failed");
				latest_table();
				cancel_edit();
			}

			
			
			function saveDone(data){
				if (data.result==1){	
				alert("Saved");
			}
			else {
				alert("Save failed");	
				}
			}
			//hides the form
			function cancel_edit(){
				//fade out the form in half a second
				$("#divEdit").fadeOut(500);
				
			}

			function cancel_add(){
				//fade out the form in half a second
				$("#divAdd").fadeOut(500);
				
			}

			function cancel_view(){
				//fade out the form in half a second
				$("#divView").fadeOut(500);
				
			}

			function next(){
				if (end<total_entries){
				$("#divNext").show();
				$("#divStatus").hide();	
				start=end;
				end+=limit;
				if (end<total_entries){
				latest_table();
			}
			else{
				end=total_entries;
				latest_table();
			}
			}
			else{
				$("#divStatus").show();	
				$("#divStatus").text("You have reached the end of the page");
			}
			
			}

			function previous(){
				if (start>0){
				$("#divPrevious").show();
				$("#divStatus").hide();		
				end=start;
				start-=limit;
				latest_table();
			}
			else{
				$("#divStatus").show();
				$("#divStatus").text("There is no user information before this page");
			}
			}


			function rotate_style(row){
				if(row%2==0){
				return " class='row1'";
				}else{
				return " class='row2'";
				}	
			}

			function latest_table(){
				var u="user_action.php?cmd=2";	
				r= syncAjax(u);		
				total_entries = r.users.length;
				console.log(total_entries);
				var table ="<table class='reportTable'><tr class='header'><td>Name</td><td>View</td><td>Edit</td><td>Delete</td></tr>";
			
				for(var i=start;i<end;i++){

					table+="<tr"+rotate_style(i)+"><td>"+r.users[i].firstname+" "+r.users[i].lastname+"</td><td>"+"<span class='hotspot' onclick='view(this,"+r.users[i].id+",event)'>view</td><td>"
					+"<span class='hotspot' onclick='edit(this,"+r.users[i].id+",event)'>edit</span></td><td>"+
					"<a href='index.php?id="+r.users[i].id+"' onclick='return confirm(\"Are you sure you want to delete?\")'>delete<a></td></tr>";

					
				}
				
				table +="<tr class='pagination'><td id='divPrevious'><span class='hotspot' onclick='previous()'>&lt; PREVIOUS PAGE </span></td><td></td><td></td><td id='divNext'><span class='hotspot' onclick='next()'>NEXT PAGE &gt;</span></td></tr></table>";

				if(document.getElementById("divContent")==null){
					console.log("Not yet initialized");
				}
				else {
				document.getElementById("divContent").innerHTML=table;
			}
				
			}

			function search_table(r){
				//var u="user_action.php?cmd=5&fn="+tx;	
				//console.log(u);
				//r= syncAjax(u);
				if (r.users!=null){	
				console.log(r);	
				var table ="<table class='reportTable'><tr class='header'><td>Name</td><td>View</td><td>Edit</td><td>Delete</td></tr>";
			
				for(var i=0;i<r.users.length;i++){

					table+="<tr"+rotate_style(i)+"><td>"+r.users[i].firstname+" "+r.users[i].lastname+"</td><td>"+"<span class='hotspot' onclick='view(this,"+r.users[i].id+",event)'>view</a></td><td>"
					+"<span class='hotspot' onclick='edit(this,"+r.users[i].id+",event)'>edit</span></td><td>"+
					"<a href='index.php?id="+r.users[i].id+"' onclick='return confirm(\"Are you sure you want to delete?\")'>delete<a></td></tr>";
					
				}
				table +="</table>";
				if(document.getElementById("divContent")==null){
					console.log("Not yet initialized");
				}
				else {
				document.getElementById("divContent").innerHTML=table;
			}
					$("#divContent").show();
					$("#divStatus").hide();
			}
				else{

					$("#divContent").hide();
					$("#divStatus").text("User not found!!");
					$("#divStatus").show();
					
				}
				
			}

		</script>

		<?php
			include("users.php");
			$obj=new users();
			include("positions.php");	
			$objPositions=new positions();
			if (isset($_REQUEST['id'])){
				$obj->delete_user($_REQUEST['id']);
			}	
			if(!$obj->get_all_users()){
			echo "error";
			exit();
			}
			$row=$obj->fetch();
		?>
	</head>
	<body>
		
		<table>
			<tr>
				<td colspan="2" id="pageheader">
					Health Information System
				</td>
			</tr>
			<tr>
				<td id="mainnav">	
					<div class="menuitem">location</div>
					<div class="menuitem">opd cases</div>
					<div class="menuitem">health promotion</div>
					<div class="menuitem">nutrition</div>
					<div class="menuitem">child welfare</div>
					<div class="menuitem">family planning</div>
					<div class="menuitem"><a href="logout.php">logout<a></div>
				</td>
				<td id="content">
					<div id="divPageMenu">
						<span class="menuitem" ><a href="index_users.php">Users</a></span>
						<span class="menuitem" ><a href="index_community_list.php">Communities</a></span>
						<span class="menuitem" ><a href="index_subdistrict_list.php">Sub districts</a></span>
						<span class="menuitem" ><a href="index_district_list.php">Districts</a></span>
						<span class="menuitem" ><a onclick="add()">Add user</a></span>
						<input type="text" id="txtSearch">
						<span class="menuitem" onclick="search()">search</span>		
					</div>
					<div id="divStatus" class="status">
	
					</div>
					<div id="divContent" name="divContent">
						<script>
						latest_table();
						</script>
					</div>
				</td>
			</tr>
		</table>



<div id="divEdit" class="popupForm">
		<table class="tableForm" >
					<?php			

							echo "<h3>Edit User Information</h3>";
					?>
					<tr>
						<td class="label">First name</td> 
						<td class="field"><input type="text" value="<?php echo $row['firstname'] ?>" name="firstname" id="firstname">
						</td>
					</tr>
					<tr>
						<td class="label">Last Name </td>
						<td class="field"><input type="text" value="<?php echo $row['lastname'] ?>" name="lastname" id="lastname">
						</td>
					</tr>
					<tr>
						<td class="label">User Name </td>
						<td class="field"><input type="text" value="<?php echo $row['user_name'] ?>" name="user_name" id="user_name">
						</td>
					</tr>
					<tr>
						<td class="label">Password </td>
						<td class="field"><input type="password" value="<?php echo $row['password'] ?>" name="password" id="password">
						</td>
					</tr>
					<tr>
						<td class="label">Position</td>
						<td class="field">

							<?php
							echo "<select name='position' id='position'>";
							if (!$objPositions->get_all_positions()){
								echo "error";

								echo mysql_error($objPositions->link);
								exit();
							}
							$row1 = $objPositions->fetch();
							
							while ($row1){
							    	echo "<option value='$row1[pid]' selected>$row1[position_name]</option>";
							$row1=$objPositions->fetch();
								} 
							
							echo "</select>";
							?>
						</td>
					</tr>					
					<tr>
						<td class="label">Status </td>
						<td class="field"><select name="status" id='status'>
							<?php $dropDownVal = $row1["status"]?>
                  <option value=0 <?php if ($dropDownVal==0) echo 'selected="selected"'; ?>>Active</option>
                  <option value=1 <?php if ($dropDownVal==1) echo 'selected="selected"'; ?>>Inactive</option>
                </select>  
						</td>
					</tr>
					<tr>
						<td class="label">Date Created </td>
						<td class="field"><input type="text" readonly="true" value="<?php echo $row['date_created'] ?>" name="date_created" id="date_created">
						</td>
					</tr>
					<tr>
						<td class="label">Phone +233-0</td>
						<td class="field"><input type="text" value="<?php echo $row['phone_number'] ?>" name="phone_number" id="phone_number">
						</td>
					</tr>
					<tr>
						<td class="label"></td>
						<td class="field">
							<input id="savid" type="button" value="save" onclick="save()" >
							<!-- <input id="addid" type="button" value="save" onclick="addNew()" > -->
							<input type="button" value="cancel" onclick="cancel_edit()" >
						</td>
					</tr>
			</table>
	</div>





	<div id="divAdd" class="popupForm">
		<table class="tableForm" >
					<tr>
						<td class="label">First name</td> 
						<td class="field"><input type="text" id="fn" name="fn">
						</td>
					</tr>
					<tr>
						<td class="label">Last Name </td>
						<td class="field"><input type="text" id="ln" name="ln">
					</tr>
					<tr>
						<td class="label">User Name </td>
						<td class="field"><input type="text" id="un" name="un">
						</td>
					</tr>
					<tr>
						<td class="label">Password </td>
						<td class="field"><input type="password" id="pw" name="pw">
						</td>
					</tr>
					<tr>
						<td class="label">Position</td>
						<td class="field">

							<?php
							echo "<select name='ps' id='ps'>";
				
							if (!$objPositions->get_all_positions()){
								echo "error";

								echo mysql_error($objPositions->link);
								exit();
							}
							$row=$objPositions->fetch();
							while ($row){
							    	echo "<option value='$row[pid]' selected>$row[position_name]</option>";
							$row=$objPositions->fetch();
								} 
							
							echo "</select>";
							?>
							</td>
					</tr>
					<tr>
						<td class="label">Status</td>
						<td class="field"><select id="st" name="st" >
                  <option value=0 <?php if ($dropDownVal==0) echo 'selected="selected"'; ?>>Active</option>
                  <option value=1 <?php if ($dropDownVal==1) echo 'selected="selected"'; ?>>Inactive</option>
                </select>  
						</td>
					</tr>
					<tr>
						<td class="label">Date Created </td>
						<td class="field"><input type="hidden" readonly="true" name="dc" id="dc">
						</td>
					</tr>
					<tr>
						<td class="label">Phone +233-0</td>
						<td class="field"><input type="text" name="pn" id="pn">
						</td>
					</tr>
					<tr>
						<td class="label"></td>
						<td class="field">
							<!-- <input id="savid" type="button" value="save" onclick="save()" > -->
							<input id="addid" type="button" value="save" onclick="addNew()" >
							<input type="button" value="cancel" onclick="cancel_add()" >
						</td>
					</tr>
			</table>
	</div>

	<div id="divView" class="popupForm">
		<table class="tableForm" >
			<?php			

							echo "<h3>View User Information</h3>";
					?>
					<tr>
						<td class="label">First name</td> 
						<td class="field"><input type="text" id="f_name" value="<?php echo $row['firstname'] ?>" readonly="true" name="f_name">
						</td>
					</tr>
					<tr>
						<td class="label">Last Name </td>
						<td class="field"><input type="text" id="l_name" value="<?php echo $row['lastname'] ?>" readonly="true" name="l_name">
					</tr>
					<tr>
						<td class="label">User Name </td>
						<td class="field"><input type="text" id="u_name" value="<?php echo $row['user_name'] ?>" readonly="true" name="u_name">
						</td>
					</tr>
					<tr>
						<td class="label">Password </td>
						<td class="field"><input type="password" id="p_word" value="<?php echo $row['password'] ?>" readonly="true" name="p_word">
						</td>
					</tr>
					<tr>
						<td class="label">Position</td>
						<td class="field">

							<?php
							echo "<select name='p_s' id='p_s' readonly='true'>";
				
							if (!$objPositions->get_all_positions()){
								echo "error";

								echo mysql_error($objPositions->link);
								exit();
							}
							$row=$objPositions->fetch();
							while ($row){
							    	echo "<option value='$row[pid]' selected>$row[position_name]</option>";
							$row=$objPositions->fetch();
								} 
							
							echo "</select>";
							?>
					</tr>
					<tr>
						<td class="label">Status</td>
						<td class="field"><select id="s_t" readonly="true" name="s_t" >
                  <option value=0 <?php if ($dropDownVal==0) echo 'selected="selected"'; ?>>Active</option>
                  <option value=1 <?php if ($dropDownVal==1) echo 'selected="selected"'; ?>>Inactive</option>
                </select>  
						</td>
					</tr>
					<tr>
						<td class="label">Date Created </td>
						<td class="field"><input type="text" readonly="true" value="<?php echo $row['date_created'] ?>" name="d_created" id="d_created">
						</td>
					</tr>
					<tr>
						<td class="label">Phone +233-0</td>
						<td class="field"><input type="text" readonly="true" value="<?php echo $row['phone_number'] ?>" name="p_number" id="p_number">
						</td>
					</tr>
					<tr>
						<td class="label"></td>
						<td class="field">
							
							<input type="button" value="cancel" onclick="cancel_view()" >
						</td>
					</tr>
			</table>
	</div>
	</body>

</html>	